package com.xthh._function;

import android.content.Context;
import android.view.View;
import android.widget.PopupWindow;

import com.luck.picture.lib.tools.ToastUtils;
import com.xthh.XTCore;
import com.xthh._ble.HH_BT;
import com.xthh._enum.HH_EnumErrorCode;
import com.xthh._function.MyPopWindow.HH_SpinerAdapter;
import com.xthh._function.MyPopWindow.HH_SpinerPopWindow;
import com.xthh._function.MyPopWindow2.HH_SpinerAdapter2;
import com.xthh._function.MyPopWindow2.HH_SpinerPopWindow2;
import com.xthh._function.MyPopWindow3.HH_SpinerAdapter3;
import com.xthh._function.MyPopWindow3.HH_SpinerPopWindow3;
import com.xthh._function.MyPopWindow3.HH_T_SpinnerRosInfo;
import com.xthh._function.MyPopWindow4.HH_SpinerAdapter4;
import com.xthh._function.MyPopWindow4.HH_SpinerPopWindow4;
import com.xthh._struct.HH_T_BleConnectResult;
import com.xthh._struct.HH_T_ScanResult;
import com.xthh._struct.HH_T_SystemParams;

import java.util.List;

public class HH_Spinner {

    public interface ResultCallBack {
        public void callback(int pos, String value);
    }
    public interface ResultCallBack3 {
        public void callback(int pos, HH_T_SpinnerRosInfo value);
    }
    public interface ResultDisBack {
        public void callback(int pos, String value);
    }
    public interface ResultDisBack4 {
        public void callback(int pos, String value);
    }

    public static void Spinner(HH_T_SystemParams spstemParams, List<String> mObjects, View showViewAsDropDown,
                               int width, int height,
                               int sel,
                               final ResultCallBack resultCallBack,
                               final ResultDisBack resultDisBack){
        if (mObjects == null || mObjects.size() <= 0)
            return;

        if (sel < 0 || sel >= mObjects.size())
            return;

        HH_SpinerAdapter mAdapter;
        HH_SpinerPopWindow mSpinerPopWindow;

        mAdapter = new HH_SpinerAdapter(spstemParams.getCtx(), mObjects, sel);



        //初始化PopWindow
        mSpinerPopWindow = new HH_SpinerPopWindow(spstemParams.getCtx(), height);
        mSpinerPopWindow.setAdatper(mAdapter);
        mSpinerPopWindow.setItemListener(new HH_SpinerAdapter.IOnItemSelectListener() {
            @Override
            public void onItemClick(int pos) {
                if (pos >= 0 && pos < mObjects.size()){
                    resultCallBack.callback(pos, mObjects.get(pos));
                    HH_SystemUtil.SendMessage(spstemParams, XTCore.MSG_SPININFO, mObjects.get(pos), pos, 0);
                }
            }
        });

        mSpinerPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                resultDisBack.callback(0, "");
            }
        });


        //设置mSpinerPopWindow显示的宽度
        mSpinerPopWindow.setWidth(width);
        //设置显示的位置在哪个控件的下方
        mSpinerPopWindow.showAsDropDown(showViewAsDropDown);
    }


    public static void Spinner2(HH_T_SystemParams spstemParams, List<String> mObjects, View showViewAsDropDown,
                               int width, int height,
                               int sel,
                               String lastData,
                               final ResultCallBack resultCallBack,
                                final ResultDisBack resultDisBack){
        if (mObjects == null || mObjects.size() <= 0)
            return;

        if (sel < 0 || sel >= mObjects.size())
            return;

        if (lastData == null || lastData.length() <= 0)
            mObjects.add("Add");
        else
            mObjects.add(lastData);

        HH_SpinerAdapter2 mAdapter;
        HH_SpinerPopWindow2 mSpinerPopWindow;

        mAdapter = new HH_SpinerAdapter2(spstemParams.getCtx(), mObjects, sel);

        //初始化PopWindow
        mSpinerPopWindow = new HH_SpinerPopWindow2(spstemParams.getCtx(), height);
        mSpinerPopWindow.setAdatper(mAdapter);
        mSpinerPopWindow.setItemListener(new HH_SpinerAdapter2.IOnItemSelectListener() {
            @Override
            public void onItemClick(int pos) {
                if (pos >= 0 && pos < mObjects.size()){
                    resultCallBack.callback(pos, mObjects.get(pos));
                    HH_SystemUtil.SendMessage(spstemParams, XTCore.MSG_SPININFO, mObjects.get(pos), pos, 0);
                }
            }
        });

        mSpinerPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                resultDisBack.callback(0, "");
            }
        });


        //设置mSpinerPopWindow显示的宽度
        mSpinerPopWindow.setWidth(width);
        //设置显示的位置在哪个控件的下方
        mSpinerPopWindow.showAsDropDown(showViewAsDropDown);
    }


    public static void Spinner3(HH_T_SystemParams spstemParams, List<HH_T_SpinnerRosInfo> mObjects, View showViewAsDropDown,
                                int width, int height,
                                int sel,
                                final ResultCallBack3 resultCallBack,
                                final ResultDisBack resultDisBack){
        if (mObjects == null || mObjects.size() <= 0)
            return;

        if (sel < 0 || sel >= mObjects.size())
            return;

        HH_SpinerAdapter3 mAdapter;
        HH_SpinerPopWindow3 mSpinerPopWindow;

        mAdapter = new HH_SpinerAdapter3(spstemParams.getCtx(), mObjects, sel);

        //初始化PopWindow
        mSpinerPopWindow = new HH_SpinerPopWindow3(spstemParams.getCtx(), height);
        mSpinerPopWindow.setAdatper(mAdapter);
        mSpinerPopWindow.setItemListener(new HH_SpinerAdapter3.IOnItemSelectListener() {
            @Override
            public void onItemClick(int pos) {
                if (pos >= 0 && pos < mObjects.size()){
                    resultCallBack.callback(pos, mObjects.get(pos));
                    HH_SystemUtil.SendMessage(spstemParams, XTCore.MSG_SPININFO, mObjects.get(pos), pos, 0);
                }
            }
        });

        mSpinerPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                resultDisBack.callback(0, "");
            }
        });


        //设置mSpinerPopWindow显示的宽度
        mSpinerPopWindow.setWidth(width);
        //设置显示的位置在哪个控件的下方
        mSpinerPopWindow.showAsDropDown(showViewAsDropDown);
    }

    public static void Spinner4(HH_T_SystemParams spstemParams, List<String> mObjects, View showViewAsDropDown,
                               int width, int height,
                               final ResultCallBack resultCallBack,
                               final ResultDisBack resultDisBack){
        if (mObjects == null || mObjects.size() <= 0)
            return;

        HH_SpinerAdapter4 mAdapter;
        HH_SpinerPopWindow4 mSpinerPopWindow;

        mAdapter = new HH_SpinerAdapter4(spstemParams.getCtx(), mObjects);


        //初始化PopWindow
        mSpinerPopWindow = new HH_SpinerPopWindow4(spstemParams.getCtx(), height);
        mSpinerPopWindow.setAdatper(mAdapter);
        mSpinerPopWindow.setItemListener(new HH_SpinerAdapter4.IOnItemSelectListener() {
            @Override
            public void onItemClick(int pos) {
                if (pos >= 0 && pos < mObjects.size()){
                    resultCallBack.callback(pos, mObjects.get(pos));
                    HH_SystemUtil.SendMessage(spstemParams, XTCore.MSG_SPININFO, mObjects.get(pos), pos, 0);
                }
            }
        });

        mSpinerPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                resultDisBack.callback(0, "");
            }
        });


        //设置mSpinerPopWindow显示的宽度
        mSpinerPopWindow.setWidth(width);
        //设置显示的位置在哪个控件的下方
        mSpinerPopWindow.showAsDropDown(showViewAsDropDown);
    }


}
